// import store from '@/store'
const common = {
  // 截取url中的code方法
  getUrlCode: function() {
    debugger
    var url = location.search;
    // this.winUrl = url;
    var theRequest = new Object();
    if (url.indexOf('?') != -1) {
      var str = url.substr(1);
      var strs = str.split('&');
      for (var i = 0; i < strs.length; i++) {
        theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
      }
    }
    return theRequest;
  },

  updateUrl: function() {
    // 生成分享的url
    let url = window.location.href.split('#')[0];
    url = this.delQueryString(url, "code");
    url = this.delQueryString(url, "state");
    url = this.delQueryString(url, "from");
    url = this.delQueryString(url, "isappinstalled");
    url = url.replace("=undefined", "");
    console.info("common/common --> updateUrl ================== url = " + url);
    window.history.pushState({}, 0, url);
  },

  // 删除url中某个参数,并跳转
  delQueryString: function(url, name) {
    var str = "";
    if (url.indexOf('?') != -1) {
      str = url.substr(url.indexOf('?') + 1);
    } else {
      return url;
    }
    var arr = "";
    var returnurl = "";
    var setparam = "";
    if (str.indexOf('&') != -1) {
      arr = str.split('&');
      for (var i in arr) {
        if (arr[i].split('=')[0] != name) {
          returnurl = returnurl + arr[i].split('=')[0] + "=" + arr[i].split('=')[1] + "&";
        }
      }
      return url.substr(0, url.indexOf('?')) + "?" + returnurl.substr(0, returnurl.length - 1);
    } else {
      arr = str.split('=');
      if (arr[0] == name) {
        return url.substr(0, url.indexOf('?'));
      } else {
        return url;
      }
    }
  },

  //如果在一个多级的json中，判断是否有某一项
  findjsonKey: function(obj, key) {
    try {
      if (typeof obj !== 'object') return false;
      if (key in obj) return true;
      for (var k in obj) {
        if (this.findjsonKey(obj[k], key)) return true;
      }
      return false;
    } catch (err) {
      console.info(err.message);
      return false;
    }
  },

  /**
   * 目标页（redirect_uri跳转页） 获取code，code来自页面返回，这个需要揣摩，隐藏的坑
   */
  getQueryString: function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
  },

  //uToast type = primary / success / error / warning / info
  toast: function(refs, title, type = 'success', duration = 2500) {
    refs.uToast.show({
      title: title,
      type: type,
      duration: duration
    })
  },

  // vuex存储
  // vuex: function(name, value) {
  //   store.commit('$uStore', {
  //     name,
  //     value
  //   })
  // }
};

export default common;